﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace NewsWorm {
    internal partial class FetchArticlesCountDialog : Form {
        internal FetchArticlesCountDialog( Feed feed, string path ) {
            InitializeComponent();
            m_Path = path;
            m_Feed = feed;
            this.Font = SystemFonts.MessageBoxFont;
            this.labelInfo.Text += " \"" + feed.Title + "\"";

            if ( feed.HasOverrideCount ) {
                m_OverrideCount = feed.OverrideCount;
            } else {
                m_OverrideCount = Properties.Settings.Default.ArticlesToFetch;
            }

            this.numericUpDownCount.Value = m_OverrideCount;
        }

        private int m_OverrideCount;
        private string m_Path;
        private Feed m_Feed;

        internal int OverrideCount { get { return m_OverrideCount; } }

        private bool WriteOverrideValue() {
            try {
                string file = m_Path + "override.dat";
                StreamWriter writer = new StreamWriter( file, true, System.Text.Encoding.Default );
                writer.WriteLine( m_Feed.Url + "||||" + m_OverrideCount.ToString() );
                writer.Close();

                return true;
            } catch ( Exception e ) {
                new MyMessageBoxError( e.Message, e.StackTrace );
                return false;
            }
        }

        private void buttonOK_Click( object sender, EventArgs e ) {
            if ( WriteOverrideValue() )
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            else
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void numericUpDownCount_ValueChanged( object sender, EventArgs e ) {
            m_OverrideCount = Convert.ToInt32( numericUpDownCount.Value );
        }
    }
}
